package com.geor.gcv.ict.common;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.lang.Lang;
import org.opencv.core.Mat;
import org.slf4j.Logger;

import com.geor.common.log.Loggers;
import com.geor.gcv.core.test.Tester;
import com.geor.gcv.core.util.FileTool;
import com.geor.gcv.ict.Classifier;
import com.geor.gcv.ict.GeneralClassifierImpl;
import com.geor.gcv.ict.SVMClassiferImpl;

/**
 * 
 * @author geor.lusongnan
 *
 */
public class ClassiferManager {
	private static Tester tester = new Tester();
	static Logger log = Loggers.get();
	private static Map<String, Classifier> CLASSIFIERS = new HashMap<String, Classifier>();

	private ClassiferManager() {
	}

	public static void loadSVM(File... trainDatas) {
		for (File trainData : trainDatas) {
			Classifier classifier = new SVMClassiferImpl();
			try {
				classifier.load(trainData.getAbsolutePath());
				CLASSIFIERS.put(FileTool.getFileName(trainData), classifier);
			} catch (Exception e) {
				log.warn("svm load --> " + trainData + " load fail");
			}
		}
	}

	public static void loadSVM(String... trainDatas) {
		for (String trainData : trainDatas) {
			Classifier classifier = new SVMClassiferImpl();
			try {
				classifier.load(trainData);
				CLASSIFIERS.put(FileTool.getFileName(new File(trainData)), classifier);
			} catch (Exception e) {
				log.warn("svm load --> " + trainData + " load fail!");
			}
		}
	}

	public static void loadGen(File dir) {
		try {
			Classifier classifier = new GeneralClassifierImpl();
			classifier.load(dir.getAbsolutePath());
			CLASSIFIERS.put(dir.getName(), classifier);
		} catch (Exception e) {
			log.warn("gen load --> " + dir.getAbsolutePath() + " load fail!");
		}
	}

	public static String predict(String key, Mat shard) {
		String response = predictSkipDebug(key, shard);
		if (response != null) {
			tester.outputShard(shard, response);
		}
		return response == null ? "?" : response;
	}

	/**
	 * 特殊情况，需要返回多个识别结果
	 * 
	 * @param key
	 * @param shard
	 * @return
	 */
	public static List<String> predicts(String key, Mat shard) {
		Classifier classifier = CLASSIFIERS.get(key);
		if (classifier instanceof GeneralClassifierImpl) {
			List<String> responses = ((GeneralClassifierImpl) classifier).predicts(shard);
			if (!Lang.isEmpty(responses)) {
				if (responses.get(0) != null) {
					tester.outputShard(shard, responses.get(0));
				}
			}
			return responses;
		}
		return null;
	}

	public static String predict(String key, Mat shard, String... contents) {
		Classifier classifier = CLASSIFIERS.get(key);
		String response = null;
		if (classifier != null) {
			response = classifier.predict(shard, contents);
		} else {
			log.error(key + " not load!");
		}
		if (response != null) {
			tester.outputShard(shard, response);
		}
		return response;
	}

	public static String predictSkipDebug(String key, Mat shard) {
		Classifier classifier = CLASSIFIERS.get(key);
		String response = null;
		if (classifier != null) {
			response = classifier.predict(shard);
		} else {
			log.error(key + " not load!");
		}
		return response;
	}

}
